package cn.bdqn.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.bdqn.dao.ProviderDao;
import cn.bdqn.pojo.Provider;
import cn.bdqn.util.PageBean;
@Service("providerService")
public class ProviderServiceImpl implements ProviderService{

	@Autowired
	private ProviderDao providerDao;
	
	@Override
	public PageBean<Provider> findByPage(int pageNo, int pageSize, String providerName, String providerCode) {
		// TODO Auto-generated method stub
		PageBean<Provider> pageBean=new PageBean<Provider>();
		pageBean.setPageSize(pageSize);
		int totalCount=providerDao.getCountByConditions(providerName, providerCode);
		pageBean.setTotalCount(totalCount);
		pageBean.setPageNo(pageNo);
		int form=(pageBean.getPageNo()-1)*pageBean.getPageSize();
		List<Provider> pageList=providerDao.findByPageConditions(form, pageSize, providerName, providerCode);
		pageBean.setPageList(pageList);
		return pageBean;
	}

	@Override
	public Provider findByCode(String proCode) {
		// TODO Auto-generated method stub
		Provider provider=providerDao.findByCode(proCode);
		return provider;
	}

	@Override
	public boolean updateByProvider(Provider provider) {
		// TODO Auto-generated method stub
		int result=providerDao.updateByProvider(provider);
		System.out.println(result);
		if(result>0){
			return true;
		}else{
			return false;
		}	
	}

	@Override
	public boolean addProvider(Provider provider) {
		// TODO Auto-generated method stub
		int result=providerDao.addProvider(provider);
		if(result>0){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean checkProCode(String proCode) {
		// TODO Auto-generated method stub
		Provider provider=providerDao.findByCode(proCode);
		if(provider!=null){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public List<Provider> findAll() {
		// TODO Auto-generated method stub
		return providerDao.findAll();
	}

	@Override
	public Object deleteById(Integer id) {
		// TODO Auto-generated method stub
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("billSum", providerDao.getBillCountById(id));
		if(providerDao.getBillCountById(id)==0){
			map.put("result", providerDao.deleteById(id));
		}else{
			map.put("result",-1);
		}
		return map;
	}

}
